package com.bawei.goods.hospital_goods.service.impl;

import com.bawei.common.utils.PageUtils;
import com.bawei.goods.hospital_goods.dao.GoodTableDao;
import com.bawei.goods.hospital_goods.entity.GoodTableEntity;
import com.bawei.goods.hospital_goods.vo.OrderModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Supplier;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bawei.common.utils.Query;

import com.bawei.goods.hospital_goods.dao.OrderListDao;
import com.bawei.goods.hospital_goods.entity.OrderListEntity;
import com.bawei.goods.hospital_goods.service.OrderListService;


@Service("orderListService")
public class OrderListServiceImpl extends ServiceImpl<OrderListDao, OrderListEntity> implements OrderListService {


    @Autowired
    private GoodTableDao tableDao;

    @Autowired
    private OrderListDao orderListDao;

    @Autowired
    private ThreadPoolExecutor executor;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<OrderListEntity> wrapper = new QueryWrapper<>();
        String starttime = (String) params.get("starttime");
        String endtime = (String) params.get("endtime");
        String receivingAForm = (String) params.get("receivingAForm");
        String payStatus = (String) params.get("payStatus");
        String orderStatus = (String) params.get("orderStatus");
        String orderNum = (String) params.get("orderNum");

        if(StringUtils.isNotEmpty(orderNum)){
            wrapper.like("order_num",orderNum);
        }

        //其实时间
        if(StringUtils.isNotEmpty(starttime)){
           wrapper.ge("create_time", starttime);
        }
        //最大时间
        if(StringUtils.isNotEmpty(endtime)){
            wrapper.le("create_time",endtime);
        }
        //发货状态
        if(!receivingAForm.equals("0")){
            wrapper.eq("receiving_a_form", receivingAForm );
        }
        //支付状态
        if(!payStatus.equals("0")){
            wrapper.eq("pay_status", payStatus );
        }
        //订单状态
        if(!orderStatus.equals("0")){
            wrapper.or().eq("order_status", orderStatus );
        }

        IPage<OrderListEntity> page = this.page(
                new Query<OrderListEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    //审核信息
    @Override
    public OrderModel getOrder(Long id) {
        OrderModel model = new OrderModel();
        //查询订单
        CompletableFuture<OrderListEntity> async = CompletableFuture.supplyAsync(() -> {
            OrderListEntity byId = this.getById(id);
            model.setOrderNum(byId.getOrderNum());
            model.setCreateTime(byId.getCreateTime());
            model.setPaymentCoding(byId.getPaymentCoding());
            model.setAggregateAmount(byId.getAggregateAmount());
            model.setActuallyPaid(byId.getActuallyPaid());
            model.setGoodQuantity(byId.getGoodQuantity());
            model.setPatientAdress(byId.getPatientAdress());
            model.setPatientName(byId.getPatientName());
            model.setPatientPhone(byId.getPatientPhone());
            model.setFreight(byId.getFreight());
            return byId;
        }, executor);

        CompletableFuture<Void> async1 = async.thenAcceptAsync((res) -> {
            //查询商品
            GoodTableEntity goodTable = tableDao.selectById(res.getGoodId());
            model.setGoodName(goodTable.getGoodName());
            model.setGoodDesc(goodTable.getGoodDesc());
            model.setGoodImg(goodTable.getGoodImg());
        }, executor);

        try {
            CompletableFuture.allOf(async,async1).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


        return model;
    }

    @Override
    public OrderModel getaudit(Long id) {
        OrderModel model = new OrderModel();
        //查询订单
        CompletableFuture<OrderListEntity> async = CompletableFuture.supplyAsync(() -> {
            OrderListEntity byId = this.getById(id);
            model.setOrderNum(byId.getOrderNum());
            model.setCreateTime(byId.getCreateTime());
            model.setPaymentCoding(byId.getPaymentCoding());
            model.setAggregateAmount(byId.getAggregateAmount());
            model.setActuallyPaid(byId.getActuallyPaid());
            model.setGoodQuantity(byId.getGoodQuantity());
            model.setPatientAdress(byId.getPatientAdress());
            model.setPatientName(byId.getPatientName());
            model.setPatientPhone(byId.getPatientPhone());
            model.setReceivingAForm(byId.getReceivingAForm());
            model.setFreight(byId.getFreight());
            model.setLogisticsInformation(byId.getLogisticsInformation());
            model.setLogisticsInformationId(byId.getLogisticsInformationId());
            return byId;
        }, executor);

        CompletableFuture<Void> async1 = async.thenAcceptAsync((res) -> {
            //查询商品
            GoodTableEntity goodTable = tableDao.selectById(res.getGoodId());
            model.setGoodName(goodTable.getGoodName());
            model.setGoodDesc(goodTable.getGoodDesc());
            model.setGoodImg(goodTable.getGoodImg());
        }, executor);

        try {
            CompletableFuture.allOf(async,async1).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public void set(OrderModel orderList) {
        QueryWrapper<OrderListEntity> wrapper = new QueryWrapper<OrderListEntity>().eq("order_num", orderList.getOrderNum());
        OrderListEntity entity = this.getOne(wrapper);
        entity.setLogisticsInformation(orderList.getLogisticsInformation());
        entity.setLogisticsInformationId(orderList.getLogisticsInformationId());
        entity.setPostageStatus(orderList.getPostageStatus());
        entity.setRemarkDesc(orderList.getRemarkDesc());

        entity.setShippingStatus(2);
        entity.setReceivingAForm(1);
        this.baseMapper.updateById(entity);

    }

    @Override
    public void uditset(OrderModel orderList) {
        QueryWrapper<OrderListEntity> wrapper = new QueryWrapper<OrderListEntity>().eq("order_num", orderList.getOrderNum());
        OrderListEntity entity = this.getOne(wrapper);

        entity.setApproverDesc(orderList.getApproverDesc());
        entity.setRefundFee(entity.getActuallyPaid().subtract(entity.getFreight()));
        entity.setOrderStatus(orderList.getStatus());

        this.baseMapper.updateById(entity);
    }


}